package com.ebo.corelib.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import com.ebo.corelib.R;
import com.ebo.corelib.ui.widget.listener.MenuInterface;
import com.ebo.corelib.ui.widget.listener.OnMenuItemClickListener;
import com.ihongqiqu.util.DisplayUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by admin on 2018/5/28.
 */

public class LinearButton<T extends MenuInterface> extends LinearLayout implements MenuButton.OnChildSeletedRefreshListener {

    private int mSeletedColor = Color.parseColor("#2ae2ff");
    private int mUnSeletedColor = Color.WHITE;
    private boolean isShowDivider = false;
    private int mDividerColor = Color.parseColor("#d8d8d8");
    private int mMenuItemBgColor = Color.WHITE;
    private int mMenuItemTextColor = Color.parseColor("#333333");
    private int mTextAlign = Gravity.CENTER;
    private int mExpandIcon = R.drawable.white_back_arrow;
    private int mTextColor = Color.WHITE;
    private int mPrvComplexBtnPosition = -1;
    private int mCurrentPosition = 0;
    private boolean isDrawBar = true;
    private int barHeight =10;
    private Paint mPaint;

    private int direction = MenuButton.VERTICAL;


    private Context mContext;
    private List<MenuButton> mChildList = new ArrayList<>();


    public LinearButton(Context context) {
        super(context);
        init(context);
    }

    public LinearButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LinearButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        if(isDrawBar){
            setWillNotDraw(false);
            mPaint = new Paint();
            mPaint.setColor(mSeletedColor);
        }
    }


    public void setDirection(int direction){
        this.direction = direction;
    }
    public void setExpandIcon(int iconid){
        this.mExpandIcon = iconid;
    }

    public void setButtons(List<T> btnModels) {
        mChildList.clear();
        int position = 1;
        for (final T model :
                btnModels) {
            LayoutParams params;
            if (getOrientation() == HORIZONTAL) {
                params = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
            } else {
                params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
            }
            params.weight = 1;
            final MenuButton<T> mb = new MenuButton<T>(mContext,direction);
            mb.setLayoutParams(params);
            mb.setText(model.getShowText());
            mb.setTag(model.getShowText());
            mb.setGravity(mTextAlign);
            mb.setTextColor(mTextColor);
            mb.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {

                    for (int i = 0; i < mChildList.size(); i++) {
                        if(view == mChildList.get(i)){
                            mCurrentPosition = i;
                            break;
                        }
                    }

                    MenuButton tmpView = (MenuButton) view;
                    if (tmpView.getAdapter() != null && tmpView.getAdapter().getCount() > 0) {
                        tmpView.defaultClickEvent(view);
                    } else {
                        refreshSelectedView(tmpView);
                        if (mListener != null)
                            mListener.onMenuClick(model, view);
                    }
                    invalidate();
                }
            });
            mChildList.add(mb);
            addView(mb);
            if (position != btnModels.size() && isShowDivider) {
                View line = new View(getContext());
                LayoutParams paramsDivider;
                if (getOrientation() == HORIZONTAL) {
                    paramsDivider = new LayoutParams(DisplayUtils.dip2px(mContext, 1), ViewGroup.LayoutParams.MATCH_PARENT);
                } else {
                    paramsDivider = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(mContext, 1));
                }
                line.setBackgroundColor(mDividerColor);
                line.setLayoutParams(paramsDivider);
                addView(line);
            }
            position++;
        }
    }

    public void setChildAdapter(int position, BaseAdapter adapter) {
        if (position >= mChildList.size()) return;
        MenuButton tmpBtn =  mChildList.get(position);
        tmpBtn.setAdapter(adapter);
        tmpBtn.setExpandIcon(mExpandIcon);
        tmpBtn.setOnChildSeletedRefreshListener(this);
    }

    private OnMenuItemClickListener mListener;

    public void setListener(OnMenuItemClickListener listener) {
        this.mListener = listener;
        for (MenuButton btn :
                mChildList) {
            btn.setListener(listener);
        }
    }


    public void refreshSelectedView(MenuButton view) {
        for (int i = 0; i < mChildList.size(); i++) {
            mChildList.get(i).setTextColor(mUnSeletedColor);
        }
        view.setTextColor(mSeletedColor);
    }


    @Override
    public void onRefresh(View view) {
        if(view instanceof MenuButton)
            refreshSelectedView((MenuButton) view);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int count  = mChildList.size();

        if(isDrawBar && count > 0){
            float trueWidth = getWidth()-getPaddingLeft()-getPaddingRight();

            float left = getX()+(trueWidth/count)*mCurrentPosition+getPaddingLeft() ;
            float bottom = getHeight();
            float top = bottom-barHeight;
            float right = left+(trueWidth/count);
            RectF rect = new RectF(left,top,right,bottom);
            canvas.drawRect(rect,mPaint);
        }
    }

    public void setmCurrentPosition(int position){
        this.mCurrentPosition = position;
        if(mChildList.size()>0)
            refreshSelectedView(mChildList.get(position));
    }
}
